package com.base.controller;

import com.base.model.ResponseBase;
import com.base.model.User;
import com.base.service.UserCenterService;
import com.base.util.JsonUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: UserCenterController
 * @Description: 用户中心接口
 * @Author: CJQ
 * @Date: 2021/2/2 9:51
 */
@Api(tags = "用户中心")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserCenterController {

    @Autowired
    private UserCenterService userCenterService;

    @ApiOperation("获取用户token")
    @GetMapping("/getToken")
    public String getToken(@RequestParam String account) {
        log.info("用户中心模块:获取用户TOKEN接口传入的参数account=" + account);
        String token = userCenterService.getToken(account);
        return "token：" + token;
    }


    /**
     * 获取用户
     *
     * @param account
     * @return
     */
    @ApiOperation("获取用户")
    @GetMapping("/getUsers/{pageNum}")
    public ResponseEntity<PageInfo<User>> getUsers(@PathVariable("pageNum") int pageNum, @RequestParam("account") String account) {
        log.debug("UserController.getUsers请求参数:account:" + account + ";pageNum:" + pageNum);
        return new ResponseEntity<PageInfo<User>>(userCenterService.getUsers(pageNum, account), HttpStatus.OK);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @ApiOperation("修改用户")
    @PostMapping("/editUser")
    public ResponseBase editUser(@RequestBody User user) {
        log.debug("UserController.editUser请求参数:user=" + JsonUtils.tojson(user));
        ResponseBase responseBase = userCenterService.editUser(user);
        return responseBase;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @ApiOperation("删除用户")
    @GetMapping("/deleteUser/{userId}")
    public ResponseBase deleteUser(@PathVariable("userId") String userId) {
        log.debug("UserController.deleteUser请求参数:userId=" + userId);
        ResponseBase responseBase = userCenterService.deleteUser(userId);
        return responseBase;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public ResponseBase addUser(@RequestBody User user) {
        log.debug("UserController.addUser请求参数:user=" + JsonUtils.tojson(user));
        ResponseBase responseBase = userCenterService.addUser(user);
        return responseBase;
    }

}
